package base;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Created by 13040443 on 2020/6/30
 */
public class ZKLock {

	public static void main(String[] args) {
		CuratorFramework client = CuratorFrameworkFactory.newClient("", 5000, 4000,
				new ExponentialBackoffRetry(300, 500));
		client.start();

		InterProcessMutex lock = new InterProcessMutex(client, "locks");
		try {
			System.out.println("抢占锁");
			// 临时有序节点 结合watch 实现分布式阻塞锁
			lock.acquire();
			System.out.println("抢到锁");
			System.out.println("do biz ");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				lock.release();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
